#include "testApp.h"

#include "twitterKeys.h"

void testApp::setup()
{
	// create an application at https://dev.twitter.com/
	// to get the application consumer key and secret
	// save TWITTER_APP_KEY and TWITTER_APP_SECRET to twitterKeys.h like this:
	// #define TWITTER_APP_KEY "YOUR_KEY"
	// #define TWITTER_APP_SECRET "YOUR_SECRET"
	twitter.setConsumerKey(TWITTER_APP_KEY);
	twitter.setConsumerSecret(TWITTER_APP_SECRET);

	string tokens_file = ofToDataPath("twitter.txt", true);
	if (!twitter.loadTokens(tokens_file))
	{
		string auth_url;
		twitter.requestToken(auth_url);
		twitter.handlePin(auth_url);
		twitter.accessToken();
		twitter.saveTokens(tokens_file);
	}

	//font.loadFont("verdana.ttf", 12, true, true, false);
	ftglFont = new FTGLPixmapFont(ofToDataPath("verdana.ttf").c_str());
	if (ftglFont->Error())
	{
		cerr << "cannot load font" << endl;
	}
	ftglFont->FaceSize(12);

	stream = new rt::Stream(twitter);

	stream->track("tweetpositive");
	stream->connect(URL_STREAM_FILTER);

	listener.setup(this);
	twitter.addEventListener(listener);
}


void testApp::update()
{
	stream->update();
}

void testApp::draw()
{
	ofBackground(255, 255, 255);
	ofSetColor(0, 0, 0);

	for (int i = 0; i < texts.size(); i++)
	{
		string s = texts[texts.size() - 1 - i];
		//font.drawString(s, 20, 20 + i * 20);

		glRasterPos2d(20, 20 + i * 20);
		ftglFont->Render(s.c_str());
	}
}

void testApp::newTweetEvent(rtt::Tweet tweet)
{
	printf("%s\n", tweet.getText().c_str());
	texts.push_back(tweet.getText());
}

void testApp::keyPressed(int key)
{
}

void testApp::keyReleased(int key)
{
}

void testApp::mouseMoved(int x, int y )
{
}

void testApp::mouseDragged(int x, int y, int button)
{
}

void testApp::mousePressed(int x, int y, int button)
{
}

void testApp::mouseReleased(int x, int y, int button)
{
}

void testApp::windowResized(int w, int h)
{
}

void testApp::gotMessage(ofMessage msg)
{
}

void testApp::dragEvent(ofDragInfo dragInfo)
{
}

void TwitterListener::onStatusUpdate(const rtt::Tweet& tweet)
{
	app->newTweetEvent(tweet);
}
